﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlAgilityPack;
using System.IO;
using System.Text;

namespace Express
{
    public partial class MainPage : UserControl
    {
        TextBox _txbDiv = new TextBox();
        string _bodyId = Guid.NewGuid().ToString();

        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            _txbDiv.Width = 200;
            _container.Children.Add(_txbDiv);
        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(String.Format("<html><body id='{0}'></body></html>", _bodyId));
            
            HtmlNode textNode = doc.CreateTextNode(GetTextHtml());
            doc.DocumentNode.ChildNodes["html"].ChildNodes["body"].AppendChild(textNode);

            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                doc.Save(sw);
            }

            _htmlContent.Text = sb.ToString();
            _webBrowser.NavigateToString(sb.ToString());
        }

        private string GetTextHtml()
        {
            string retHtml = String.Format(@"<div>
                                                {0}
                                            </div>", _txbDiv.Text);

            return retHtml;
        }
    }
}
